<template>
  <div class="tree-tools" style="width: 100%">
    <el-row type="flex" justify="space-between">
      <el-col>
        <i class="el-icon-platform-eleme" style="margin-right: 8px"></i>
        {{ data.name }}
      </el-col>
      <el-col :span="4">
        <el-row type="flex" style="text-align: right">
          <el-col style="margin-right: 10px">{{ data.manager }}</el-col>
          <el-col>
            <el-dropdown @command="handlerCommand">
                      <span class="el-dropdown-link">
                      操作<i class="el-icon-arrow-down el-icon--right"></i>
                      </span>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item command="add">添加子部门</el-dropdown-item>
                <el-dropdown-item v-if="!isRoot" command="check">查看部门</el-dropdown-item>
                <el-dropdown-item v-if="!isRoot" command="del">删除部门</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </el-col>
        </el-row>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import {delDepartmentApi} from "@/api/departments";

export default {
  name: "treeTools",
  props: {
    data: {
      type: Object,
      default() {
        return {}
      }
    },
    // 判断是否是头部的列
    isRoot: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    //处理操作下拉元素的点击事件
    async handlerCommand(command) {
      // console.log(command);
      if (command === 'del') {
        //删除操作
        try {
          await this.$confirm('是否删除当前部门', '提示')
          await delDepartmentApi(this.data.id)
          // 调用父组件的方法更新页面数据
          this.$emit('listUpdate')
          //提示用户删除成功
          this.$message.success('删除部门成功')
        } catch {
          console.log('取消删除 || 删除出错')
        }
      } else if (command === 'add') {
        //弹出新增部门的弹框,顺带 id   //type保存操作类型
        this.$emit('showDialog', {...this.data,type:'add'})
        // console.log(this.data.id)
      }else{
        //查看编辑部门  弹出编辑部门的弹框 //type保存操作类型
        this.$emit('showDialog', {...this.data,type:'edit'})
      }
    }
  }
}
</script>

<style scoped>

</style>
